package com.kingdee.eas.hr.emp.app;

import java.util.Date;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.hr.base.EnrollSourceFactory;
import com.kingdee.eas.hr.base.EnrollSourceInfo;
import com.kingdee.eas.hr.emp.PersonPositionInfo;

public class PersonPositionControllerBeanEx extends PersonPositionControllerBean {

	@Override
	protected IObjectPK _addnew(Context ctx, IObjectValue model) throws BOSException, EASBizException {
		IObjectPK pk = super._addnew(ctx, model);
		PersonPositionInfo info = (PersonPositionInfo) model;
		Date joinDate = info.getEnterDate();
//		Date exitDate = info.getLeftDate();

		PersonInfo personInfo = info.getPerson();
		if (personInfo != null) {
			personInfo = PersonFactory.getLocalInstance(ctx).getPersonInfo(new ObjectUuidPK(personInfo.getId()));
			personInfo.setJoinJobDate(joinDate);
//			personInfo.setExitDate(exitDate);

			EnrollSourceInfo enrollInfo = this.getEnrollSource(ctx, info.getEnrollSource());
			if (enrollInfo != null) {
				personInfo.setJoinStatus(enrollInfo.getName());
			}

			PersonFactory.getLocalInstance(ctx).updatePartial(personInfo, getSelectorItemCollection());
		}
		return pk;
	}

	
	@Override
	protected void _update(Context ctx, IObjectPK pk, IObjectValue model) throws BOSException, EASBizException {
		super._update(ctx, pk, model);
		PersonPositionInfo info = this.getPersonPositionInfo(ctx, pk);
		Date joinDate = info.getEnterDate();
//		Date exitDate = info.getLeftDate();

		PersonInfo personInfo = info.getPerson();
		if (personInfo != null) {
			personInfo = PersonFactory.getLocalInstance(ctx).getPersonInfo(new ObjectUuidPK(personInfo.getId()));
			personInfo.setJoinJobDate(joinDate);
//			personInfo.setExitDate(exitDate);
			EnrollSourceInfo enrollInfo = this.getEnrollSource(ctx, info.getEnrollSource());
			if (enrollInfo != null) {
				personInfo.setJoinStatus(enrollInfo.getName());
			}
			PersonFactory.getLocalInstance(ctx).updatePartial(personInfo, getSelectorItemCollection());
		}
	}

	private SelectorItemCollection getSelectorItemCollection() {
		SelectorItemCollection selectors = new SelectorItemCollection();
		selectors.add("joinJobDate");
		selectors.add("exitDate");
		selectors.add("joinStatus");
		return selectors;
	}

	private EnrollSourceInfo getEnrollSource(Context ctx, EnrollSourceInfo info) throws BOSException, EASBizException {
		if (info != null) {
			return EnrollSourceFactory.getLocalInstance(ctx).getEnrollSourceInfo(new ObjectUuidPK(info.getId()));
		}
		return null;
	}
}
